﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreData;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmMultiSelectBranch : DevExpress.XtraEditors.XtraForm
    {
        public List<long> SeletedIDs { get; set; }
        public List<String> SeletedNames { get; set; }
         string[] BranchAr { get; set; }
        public frmMultiSelectBranch(string Branch)
        {
            InitializeComponent();
            if(!String.IsNullOrEmpty(Branch))
            BranchAr = Branch.Split(',');
            LoadData();
        }

        public string SelectName()
        {
            var t = "";
            foreach (var item in SeletedNames)
            {
                t += item + ",";
            }
            if(t!="")
            t = t.Remove(t.Length - 1, 1);
            return t;
        }
        void LoadData()
        {
            var model = RegisterService.RegisterServices.BranchService;
            gridControl1.DataSource = model.GetList();
            gridControl1.RefreshDataSource();
            gridView1.ViewCaption = "Danh sách có " + gridView1.RowCount + " dòng";

            PropertyInfo[] propertyInfos;
            propertyInfos = new Branch().GetType().GetProperties(); ;

            foreach (PropertyInfo proInfo in propertyInfos)
            {
                AttributeCollection attributes = TypeDescriptor.GetProperties(new Branch().GetType())[proInfo.Name].Attributes;

                DescriptionAttribute myAttribute =
                   (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];

                try
                {
                    if (String.IsNullOrEmpty(myAttribute.Description))
                    {
                        gridView1.Columns[proInfo.Name].Visible = false;
                    }
                    else
                    {
                        gridView1.Columns[proInfo.Name].Caption = myAttribute.Description;
                        gridView1.Columns[proInfo.Name].OptionsColumn.AllowEdit = false;
                    }
                }
                catch {  }
            }

            gridView1.Columns["SelectRowExt"].OptionsColumn.AllowEdit = true;
          //  gridView1.Columns["SelectRowExt"].VisibleIndex = 0;
            gridView1.Columns["ParentExt"].GroupIndex = 1;
            gridView1.ExpandAllGroups();


            if (BranchAr != null && BranchAr.Length > 0)
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    try
                    {
                        var index = Array.IndexOf(BranchAr, gridView1.GetRowCellValue(i, "Name").ToString());
                        if (index < 0)
                        {
                            gridView1.SetRowCellValue(i, "SelectRowExt", false);
                        }
                        else
                        {
                            gridView1.SetRowCellValue(i, "SelectRowExt", true);
                        }
                    }
                    catch { }
                }
            }

        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.RowCount != 0)
            {
               
             //   this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        int FocusedRow
        {
            get
            {
                return gridView1.FocusedRowHandle;
            }
        }

        private void cmdselected_Click(object sender, EventArgs e)
        {
            SeletedIDs = new List<long>();
            SeletedNames = new List<string>();
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                try
                {
                    var check = (bool)gridView1.GetRowCellValue(i, "SelectRowExt");
                    if (check == true)
                    {
                        SeletedIDs.Add((long)gridView1.GetRowCellValue(i, "ID"));
                        SeletedNames.Add((String)gridView1.GetRowCellValue(i, "Name"));
                    }
                }
                catch { }
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                 var _row = gridView1.GetSelectedRows();
                 for (int i = 0; i < _row.Length; i++)
                 {
                     try
                     {
                         if (((bool)gridView1.GetRowCellValue(_row[i], "SelectRowExt")) == true)
                         {
                             gridView1.SetRowCellValue(_row[i], "SelectRowExt", false);
                         }
                         else
                         {
                             gridView1.SetRowCellValue(_row[i], "SelectRowExt", true);
                         }
                     }
                     catch
                     {
                     }
                 }
             
            }
        }

    }
}
